package com.tech.freak.pyp;

import java.util.Arrays;
import java.util.HashSet;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.TextView;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlManager;
import com.adwhirl.AdWhirlTargeting;
import com.adwhirl.AdWhirlLayout.AdWhirlInterface;
import com.adwhirl.util.AdWhirlUtil;

public class Pyp extends Activity implements AdWhirlInterface{
	
	private static final String 		TAG = "Pico y Placa";
	
	private Context mContext;

	private TextView mPypLabel;
	private TextView mPypImage;
	
	private String mCity;
	private String mLicense;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "onCreate");
        
        //Save context
        mContext = this;
        
        //Create ad
        addAdvertisingView();
        
        //Get saved info
        mCity = Utils.getInfo(mContext, Utils.CITY);
        mLicense = Utils.getInfo(mContext, Utils.LICENSE);
        
		mPypLabel = (TextView) findViewById(R.id.pyp_label);
		mPypImage = (TextView) findViewById(R.id.pyp_icon);
		
        //Get city_spinner
        Spinner city_spinner = (Spinner) findViewById(R.id.city_spinner);
        ArrayAdapter<CharSequence> city_adapter = ArrayAdapter.createFromResource(
                this, R.array.cities, android.R.layout.simple_spinner_item);
        city_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        city_spinner.setAdapter(city_adapter);
        
        //Restore last value
        if(mCity!=null && !mCity.equalsIgnoreCase("")){
        	city_spinner.setSelection(city_adapter.getPosition(mCity));
        }
        
        city_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				mCity = parent.getItemAtPosition(pos).toString();
				Utils.saveInfo(mContext, Utils.CITY, mCity);
				displayPyP();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        
        //Get license spinner
        Spinner license_spinner = (Spinner) findViewById(R.id.license_spinner);
        ArrayAdapter<CharSequence> license_adapter = ArrayAdapter.createFromResource(
                this, R.array.licenses, android.R.layout.simple_spinner_item);
        license_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        license_spinner.setAdapter(license_adapter);
        
        //Restore last value
        if(mLicense!=null && !mLicense.equalsIgnoreCase("")){
        	license_spinner.setSelection(license_adapter.getPosition(mLicense));
        }
        
        license_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				mLicense = parent.getItemAtPosition(pos).toString();
				Utils.saveInfo(mContext, Utils.LICENSE, mLicense);
				displayPyP();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
    }

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}


	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		Log.d(TAG, "onResume");
		super.onResume();
		
		displayPyP();
	}
	
	protected void displayPyP(){
		Log.d(TAG, "displayPyP()");
		boolean pyp=false;
		pyp = Utils.calculatePyP(this,mCity, mLicense);
		//Show correct view
		Resources res = getResources();
		if(pyp){
			mPypLabel.setText(R.string.pypLabel);
			if(mCity.equalsIgnoreCase(Utils.BOGOTA)){
				mPypImage.setBackgroundResource(R.drawable.bogota_sad);
				String [] pypArray = res.getStringArray(R.array.pypbogota);
				String text = Utils.getLicenseText(pypArray);				
				mPypImage.setText(text);
			}else if (mCity.equalsIgnoreCase(Utils.MEDELLIN)){
				mPypImage.setBackgroundResource(R.drawable.medellin_sad);
				String [] pypArray = res.getStringArray(R.array.pypmedellin);
				String text = Utils.getLicenseText(pypArray);				
				mPypImage.setText(text);
			}
			
		}else{
			mPypLabel.setText(R.string.no_pyplabel);
			if(mCity.equalsIgnoreCase(Utils.BOGOTA)){
				mPypImage.setBackgroundResource(R.drawable.bogota_happy);
				String [] pypArray = res.getStringArray(R.array.pypbogota);
				String text = Utils.getLicenseText(pypArray);				
				mPypImage.setText(text);
			}else if (mCity.equalsIgnoreCase(Utils.MEDELLIN)){
				mPypImage.setBackgroundResource(R.drawable.medellin_happy);
				String [] pypArray = res.getStringArray(R.array.pypmedellin);
				String text = Utils.getLicenseText(pypArray);				
				mPypImage.setText(text);
			}
		}
		
	}
	
	private void addAdvertisingView() {
		
		final int DIP_HEIGHT = 52;
		final int DIP_WIDTH = 320;
		final float DENSITY = getResources().getDisplayMetrics().density;
		int scaledHeight = (int) (DENSITY * DIP_HEIGHT + 0.5f);
		int scaledWidth = (int) (DENSITY * DIP_WIDTH + 0.5f);
		
		//Set adwhirl layout
		LinearLayout layout = (LinearLayout) findViewById(R.id.main_container);
		AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, "cb97c250f5a845d683a9a6fe6b6a30e2");
		adWhirlLayout.setAdWhirlInterface(this);
		adWhirlLayout.setMaxHeight(scaledHeight);
		adWhirlLayout.setMaxWidth(scaledWidth);
		RelativeLayout.LayoutParams adWhirlLayoutParams =
		new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT,
				scaledHeight);
		layout.addView(adWhirlLayout, 0, adWhirlLayoutParams);
		
		//Configure ad properties
		String keywords[] = { "trafico", "traffic", "colombia","bogota","medellin","latino", "pico y placa" };
	    AdWhirlTargeting
	        .setKeywordSet(new HashSet<String>(Arrays.asList(keywords)));
		
	    // Optional, will fetch new config if necessary after five minutes.
	    AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
		
		//Invalidate layout so that it gets redrawn
		layout.invalidate();

	}

	@Override
	public void adWhirlGeneric() {
		Log.e(AdWhirlUtil.ADWHIRL, "In adWhirlGeneric()");	
	}
}